package com.gdpu.liou.symphonyserver.service.impl;


import com.gdpu.liou.symphonyserver.mapper.SingerMapper;
import com.gdpu.liou.symphonyserver.entity.Singer;
import com.gdpu.liou.symphonyserver.service.SingerService;
import com.gdpu.liou.symphonyserver.common.utils.PagingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 歌手service实现类
 */
@Service
public class SingerServiceImpl implements SingerService {

    @Autowired
    private SingerMapper singerMapper;


    /**
     * 增加歌手
     */
    @Override
    public boolean insert(Singer singer) {
        return singerMapper.insert(singer) > 0;
    }

    /**
     * 修改歌手
     */
    @Override
    public boolean update(Singer singer) {
        return singerMapper.update(singer) > 0;
    }

    /**
     * 删除歌手
     */
    @Override
    public boolean delete(Integer id) {
        return singerMapper.delete(id) > 0;
    }

    /**
     * 查询歌手：根据主键
     */
    @Override
    public Singer selectBySingerId(Integer id) {
        return singerMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询所有歌手
     */
    @Override
    public List<Singer> selectAllSinger() {
        return singerMapper.selectAllSinger();
    }

    /**
     * 查询歌手集合：根据名字模糊查询
     */
    @Override
    public List<Singer> selectByName(String name) {
        return singerMapper.selectByName(name);
    }

    /**
     * 查询歌手集合：根据性别
     */
    @Override
    public List<Singer> selectBySex(Integer sex) {
        return singerMapper.selectBySex(sex);
    }


    /**
     * 分页查询歌手
     */
    @Override
    public PagingUtil pagingQuerySinger(Integer currentPage, String name) {
        Integer totalCount = singerMapper.selectTotalSinger(name);
        Integer pageSize = 6;
        int pageCount = 0;
        pageCount = (totalCount % pageSize == 0 ? (totalCount / pageSize) : (totalCount / pageSize) + 1);
//        数据起始位置
        int start = (currentPage - 1) * pageSize;
        List<Singer> data = singerMapper.selectPagingData(start, pageSize , name);
        PagingUtil pagingUtil = new PagingUtil(pageSize, totalCount, pageCount, currentPage, data);
        return pagingUtil;
    }

    @Override
    public List<Singer> selectSingerSelected() {
        return singerMapper.selectSingerSelected();
    }
}
